export default class MediaDevices {
	constructor(updateDevice) {
		this.micDevices = [];
		this.speakerDevices = [];
		this.videoDevices = [];
		this.allMediaDevices = [];
		this.init();
		this.updateDevice = updateDevice;
	}

	async init() {
		await this.getUserDevicesEnum();
		this.addUserDeviceUpdate();
	}

	async getUserDevicesEnum() {
		try {
			const userDevices = await navigator.mediaDevices.enumerateDevices();
			this.setAllMediaDevices(userDevices);
		} catch (err) {
			console.error('get user devices failed');
		}
	}

	/** 设置麦克风设备列表 */
	setMicDevices() {
		this.micDevices = this.getAllMediaDevices()
			.filter(device => device.kind === 'audioinput')
			.map(device => ({ id: device.id, label: device.label }));
	}

	/** 获取麦克风设备列表 */
	getMicDevices() {
		return this.micDevices;
	}

	/** 设置扬声器设备列表 */
	setSpeakerDevices() {
		this.speakerDevices = this.getAllMediaDevices()
			.filter(devices => devices.kind === 'audiooutput')
			.map(device => ({ id: device.id, label: device.label }));
	}
	/**获取扬声器设备列表 */
	getSpeakerDevices() {
		return this.speakerDevices;
	}

	/** 设置视频设备列表 */
	setVideoDevices() {
		this.videoDevices = this.getAllMediaDevices()
			.filter(device => device.kind === 'videoinput')
			.map(device => ({ id: device.id, label: device.label }));
	}

	/** 获取视频设别列表 */
	getVideoDevices() {
		return this.videoDevices;
	}

	/** 设置所有媒体设备 */
	async setAllMediaDevices(devices) {
		const result = devices.map(device => ({ id: device.groupId, label: device.label, kind: device.kind }));
		this.allMediaDevices = this.uniqueDevice(result);
		this.setMicDevices();
		this.setSpeakerDevices();
		this.setVideoDevices();
	}

	/** 获取所有列表枚举值 */
	getAllMediaDevices() {
		return this.allMediaDevices;
	}

	/** 监听设备列表发生变化事件 */
	addUserDeviceUpdate() {
		navigator.mediaDevices.addEventListener('devicechange', async event => {
			const result = await event.target.enumerateDevices();
			const allDevices = result.map(device => ({ id: device.groupId, label: device.label, kind: device.kind }));
			const devices = this.uniqueDevice(allDevices);
			this.getDeviceChange(devices);
		});
	}

	/** 移除设备列表发生变化事件 */
	removeUserDeviceUpdate() {
		navigator.mediaDevices.reomveEventListener('devicechange', e => {
			console.log(e);
		});
	}

	/** 设备列表发生变化 */
	getDeviceChange(devices) {
		const allDeivices = this.getAllMediaDevices();
		let result = this.getDiffKey(allDeivices, devices, 'id');
		this.updateDevice(result);
		this.getUserDevicesEnum();
	}

	/** 求差值 */
	getDiffDevice(allDevices, updateDevices, key) {
		let result = { type: 'remove', devices: [] };
		let temp = [];
		if (allDevices.length < updateDevices.length) {
			temp = updateDevices;
			updateDevices = allDevices;
			allDevices = temp;
			result.type = 'add';
		}
		for (let i = allDevices.length - 1; i >= 0; i--) {
			let flag = false;
			for (let j = 0; j < updateDevices.length; j++) {
				if (allDevices[i][key] === updateDevices[j][key]) {
					flag = true;
				}
			}
			if (!flag) {
				result.devices.push(allDevices[i]);
			}
		}
		return result;
	}

	/** 根据属性去重 */
	uniqueDevice(devices) {
		for (let i = 0; i < devices.length; i++) {
			for (let j = i + 1; j < devices.length; j++) {
				if (devices[i].id == devices[j].id) {
					devices.splice(j, 1);
					j--;
				}
			}
		}
		return devices;
	}
}
